<?php
namespace app\common\model;
use app\common\traits\Models;
use think\Model;
use think\facade\{
    App,Request,Config
};
class ComStepbystep extends Model
{
    use Models;
    protected $readonly = [];
    protected $auto     = [];
    protected $insert   = ['ip','token','end_time'];
    protected $update   = [];

    //设置Ip
    protected function setIpAttr()
    {
        return Request::ip(1);
    }

    //设置token
    protected function setTokenAttr()
    {
        return com_token();
    }

    //设置过期时间
    protected function setEndTimeAttr()
    {
        return Request::time()+Config::get('type.expire_time');
    }

    /**
     * 验证上一步下一步token
     * @param  [type] $phone [手机号码]
     * @param  [type] $step_token [token]
     * @param  [type] $type  [类型, 0:找回密码]
     * @return [type]        [description]
     */
    public function checkToken($phone, $step_token, $type=0){
        $rs = $this::get(function($query)use($phone, $step_token,$type){
            $query->where([['phone','=',$phone],['token','=',$step_token],['type','=',$type],['end_time','>',Request::time()]]);
        });
        if($rs){
            $rs->delete(true);
            return true;
        }
        $this->error = '上一步验证token无效';
        return false;
    }
}
